//
//  HDTabViewController.m
//  healthyexpert
//
//  Created by Liu Penny on 15/1/28.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDTabViewController.h"
#import "HZGoodsDetailViewController.h"
#import "HZMallGoodsListViewController.h"
#import "HZMallGoodsTemaiListViewController.h"
#import "HZGoodsSalesViewController.h"
#import "HDChatLIstController.h"
#import "SNSystemNotifyController.h"
#import "ParticipationViewController.h"

@interface HDTabViewController ()


@end

@implementation HDTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //     Do any additional setup after loading the view.
    
    UITabBar *tabBar = self.tabBar;
    
    
    for (UITabBarItem *item in tabBar.items) {
        
        item.imageInsets = UIEdgeInsetsMake(6, 0, -6,0);
        
    }
    
    
    UITabBarItem *firstItem = [[tabBar items] objectAtIndex:0]; 
    firstItem.selectedImage = [[UIImage imageNamed:@"health_pur"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    firstItem.image = [[UIImage imageNamed:@"health_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    firstItem.tag = 0;

    UITabBarItem *secondItem = [[tabBar items] objectAtIndex:1];
    secondItem.selectedImage = [[UIImage imageNamed:@"join_pur"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    secondItem.image = [[UIImage imageNamed:@"join_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    secondItem.tag = 1;

    UITabBarItem *thirdItem = [[tabBar items] objectAtIndex:2];
    thirdItem.selectedImage = [[UIImage imageNamed:@"ic_menu_opendoor_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    thirdItem.image = [[UIImage imageNamed:@"ic_menu_opendoor_default"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    thirdItem.tag = 2;
    
    UITabBarItem *forthItem = [[tabBar items] objectAtIndex:3];
    forthItem.selectedImage = [[UIImage imageNamed:@"shop_pur"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    forthItem.image = [[UIImage imageNamed:@"shop_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    forthItem.tag = 3;
    
    UITabBarItem *fifthItem = [[tabBar items] objectAtIndex:4];
    fifthItem.selectedImage = [[UIImage imageNamed:@"my_pur"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    fifthItem.image = [[UIImage imageNamed:@"my_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    fifthItem.tag = 4;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(allRedPoint) name:@"allRedPoint" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removePeoRedPoint) name:@"removePeoRedPoint" object:nil];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(numOfProductsInCartsChanged:) name:kNotifacationUserProductNumInCartsChanged object:nil];
}




- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self numOfProductsInCartsChanged:nil];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    /** 每当用户进来的时候检查有没有推送的消息  */
    [self checkRemoteNotification];
}

- (void)checkRemoteNotification{
//    [UIColor orangeColor]
    /** 读取本地暂时存储的消息  */
//    NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"kRemoteNotificationUserInfoData"];
    NSString *jumpType = [[NSUserDefaults standardUserDefaults] objectForKey:@"kRemoteNotificationUserInfoType"];
    
    /** 删除本地暂时存储的消息 -write by khzliu */
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"kRemoteNotificationUserInfoType"];
    [[NSUserDefaults standardUserDefaults] setObject:@{} forKey:@"kRemoteNotificationUserInfoData"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    
    /** 处理本地暂时存储的消息 -write by khzliu */
    if([jumpType isEqualToString:@"temaidata"]){
        /** 跳转特卖列表   */
        [self teMaiActivity:nil];
    }
    
    
}
#pragma mark - 跳转到商品详情
-(void)presentCardView:(NSDictionary*)pmidDictionary
{
    HZGoodsDetailViewController *forumVC = [[HZGoodsDetailViewController alloc]init];
    forumVC.shopID = [NSString stringWithFormat:@"%@",[pmidDictionary objectForKey:@"shopid"]];
    forumVC.productID = [NSString stringWithFormat:@"%@",[pmidDictionary objectForKey:@"productid"]];
    forumVC.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = NO;
    [(UINavigationController*)self.selectedViewController pushViewController:forumVC animated:YES];
}
#pragma mark -跳转到活动
-(void)activity:(NSDictionary*)pmidDictionary
{
    
    HZMallGoodsListViewController *mallGoodsListVC = [[HZMallGoodsListViewController alloc] init];
    mallGoodsListVC.actID = [NSString stringWithFormat:@"%@",[pmidDictionary objectForKey:@"actid"]];
    mallGoodsListVC.actName = [pmidDictionary objectForKey:@"actname"];
    if (![mallGoodsListVC.actID isEqualToString:@"-1"]) {
        mallGoodsListVC.hidesBottomBarWhenPushed = YES;
        self.navigationController.navigationBar.hidden = NO;
        [(UINavigationController*)self.selectedViewController pushViewController:mallGoodsListVC animated:YES];
    }
}
#pragma mark - 跳转到系统
-(void)teMaiActivity:(NSDictionary*)pmidDictionary
{
   
    ParticipationViewController * systemNotifyVC = [[ParticipationViewController alloc] init];
    systemNotifyVC.APNSKey = YES;
//    systemNotifyVC.hidesBottomBarWhenPushed = YES;
//    self.navigationController.navigationBar.hidden = NO;
    self.selectedIndex = 1;
  


}
#pragma makr -  跳转到web
-(void)presentView:(NSDictionary*)pmidDictionary
{
    ParticipationViewController * systemNotifyVC = [[ParticipationViewController alloc] init];
    //    systemNotifyVC.titleStr = [pmidDictionary objectForKey:@"title"];
    //    systemNotifyVC.contentStr = [pmidDictionary objectForKey:@"content"];
    systemNotifyVC.APNSKey = YES;

    
 
    systemNotifyVC.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = NO;
   [(UINavigationController*)self.selectedViewController pushViewController:systemNotifyVC animated:YES];
    
}
#pragma mark - 跳转到聊天列表
-(void)presentChatListView:(NSDictionary*)pmidDictionary
{
    HDChatLIstController * chatList = [[HDChatLIstController alloc]init];
    chatList.tempPmids = [pmidDictionary objectForKey:@"pmid"];
    chatList.chatSign = @"0";
    chatList.hidesBottomBarWhenPushed = YES;
    
    self.navigationController.navigationBar.hidden = NO;
    [(UINavigationController*)self.selectedViewController pushViewController:chatList animated:YES];
//    [self.navigationController pushViewController:chatList animated:YES];

}




/** 跟新购物车小红点 -wirte by khzliu */
- (void)numOfProductsInCartsChanged:(NSNotification *)notice{
    NSInteger productNum = [[[NSUserDefaults standardUserDefaults] objectForKey:kHZUserProductNumInCarts] integerValue];
    UITabBarItem *forthItem = [[self.tabBar items] objectAtIndex:3];
    
    if(productNum > 0){
        if (productNum >= 100) {
            productNum = 99;
        }
        forthItem.image = [[UIImage imageNamed:@"shop_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self showBadgeOnItemIndex:3 num:[NSString stringWithFormat:@"%d", productNum]];
        
    }else{
        forthItem.image = [[UIImage imageNamed:@"shop_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self removeBadgeOnItemIndex:3];
        
    }
    
}




-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag) {
        case 0:
            
            break;
        case 1:
            
            break;
        case 2:
            
            break;
        case 3:{
            [self hideBadgeOnItemIndex:3];
        }
            
            break;
            
        case 4:{
            
//            [self hideBadgeOnItemIndex:4];
        }
            break;
            
        default:
            break;
    }
    
}

-(void)allRedPoint{
    
    [self showBadgeOnItemIndex:3];
    
    
}
-(void)removePeoRedPoint
{
    [self hideBadgeOnItemIndex:4];
}

- (void)showBadgeOnItemIndex:(int)index num:(NSString *)num{
    
    [self removeBadgeOnItemIndex:index];
    
        
    UILabel *badgeView = [[UILabel alloc] init];
    badgeView.textColor = [UIColor whiteColor];
    badgeView.font = [UIFont systemFontOfSize:10];
    
    badgeView.tag = 888 + index;
    badgeView.layer.masksToBounds = YES;
    badgeView.layer.cornerRadius = 16/2;
    
    badgeView.backgroundColor = [UIColor orangeColor];
    
    CGRect tabFrame = self.tabBar.frame;
    
    float percentX = (index +0.6) / 5;
    
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    
    badgeView.frame = CGRectMake(x, y, 16, 16);
    
    badgeView.textAlignment = NSTextAlignmentCenter;
    
    badgeView.text = num;
    
    [self.tabBar addSubview:badgeView];
        
        
    
}

- (void)showBadgeOnItemIndex:(int)index{
    
    [self removeBadgeOnItemIndex:index];

    
    UIView *badgeView = [[UIView alloc]init];
    
    badgeView.tag = 888 + index;
    
    badgeView.layer.cornerRadius = 5;
    
    badgeView.backgroundColor = [UIColor orangeColor];
    
    CGRect tabFrame = self.tabBar.frame;
    
    float percentX = (index +0.6) / 4;
    
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    
    badgeView.frame = CGRectMake(x, y, 10, 10);
    
    [self.tabBar addSubview:badgeView];
   
    
}

- (void)hideBadgeOnItemIndex:(int)index{
    
    [self removeBadgeOnItemIndex:index];
    
}

- (void)removeBadgeOnItemIndex:(int)index{
    
    for (UIView *subView in self.tabBar.subviews) {
        
        if (subView.tag == 888+index) {
            
            [subView removeFromSuperview];
            
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
